package com.example.util;

import android.content.Context;
import android.util.Log;

public class PreferenceUtil {

    public static void putString(Context context, String key, String value) {
        new SecurePreferences(context).edit().putString(key, value).commit();
	}
	
    public static String getString(Context context, String key) {
    	return getString(context, key, null);
    }
    
	public static String getString(Context context, String key, String defaultValue) {
        return new SecurePreferences(context).getString(key, defaultValue);
	}
	
	public static void claerPasscode(Context context) {
		PreferenceUtil.putString(context, "Email", "");
		PreferenceUtil.putString(context, "Password", "");
		PreferenceUtil.putString(context, "Passcode", "");
		PreferenceUtil.putString(context, "Timestamp", "");
	}

	public static boolean isAvailablePasscode(Context context) {
		String email = getString(context, "Email");
		String password = getString(context, "Password");
		String passcode = getString(context, "Passcode");
		String timestamp = getString(context, "Timestamp");
		
		Log.d("PreferenceUtil.isAvailablePasscode", "email=" + email + " password=" + password + " passcode=" + passcode + " timestamp=" + timestamp);
		
		if (!StringUtil.isEmpty(email) 
				&& !StringUtil.isEmpty(password)
				&& !StringUtil.isEmpty(passcode)
				&& !StringUtil.isEmpty(timestamp)
				&& !DateUtil.isExpired(timestamp)) {
			return true;
		}
		
		return false;
	}
	
	public static boolean isExpired(Context context) {
		String timestamp = getString(context, "Timestamp");
		
		if (!StringUtil.isEmpty(timestamp) && DateUtil.isExpired(timestamp))
			return true;
		
		return false;
	}

	public static void savePasscode(Context context, String email, String password, String passcode) {
		PreferenceUtil.putString(context, "Email", email);
		PreferenceUtil.putString(context, "Password", password);
		PreferenceUtil.putString(context, "Passcode", passcode);
//		PreferenceUtil.putString(context, "Timestamp", DateUtil.getTimestamp(Const.PASSCODE_DAYS));
	}
}
